#!/bin/bash

# traverse <src_dir> <filter_func> <filter_arg> <exec_func> <exec_arg>
# traverse <src_dir> <filter_func> <filter_arg> <exec_install>   <strip_dir_num> <install_dst_dir>
# traverse <src_dir> <filter_func> <filter_arg> <exec_uninstall> <strip_dir_num> <uninstall_dst_dir>

function traverse()
{
	local dir=$1
	local filter_func=$2
	local filter_arg=$3
	local exec_func=$4
	shift 4

	local file
	local current
	for file in $(ls ${dir})
	do
		current=${dir}/${file}
		if [[ -d ${current} ]]; then
			${FUNCNAME} ${current} ${filter_func} ${filter_arg} ${exec_func} $@
		else
			${filter_func} ${current} ${filter_arg}
			if [[ $? -eq 0 ]]; then
				${exec_func} ${current} $@
			fi
		fi
	done
}

function traverse_filter_none()
{
	return 0
}

function traverse_filter_filename()
{
	local filename=$1
	local keywords=$2
	if [[ $(basename ${filename}) =~ ${keywords} ]]; then
		return 0
	else
		return 1
	fi
}

function traverse_filter_filename_r()
{
	local filename=$1
	local keywords=$2
	if [[ $(basename ${filename}) =~ ${keywords} ]]; then
		return 1
	else
		return 0
	fi
}

# /A/B/xxxx   strip_dir_num=3
# ../A/B/xxx  strip_dir_num=3
function traverse_exec_install()
{
#	echo -e "[DEBUG] ${FUNCNAME} $@"

	local src_file=$1
	local strip_dir_num=$2
	local dst_dir=$3

	local src_file_name=$(basename ${src_file})

	echo -e "[INFO] >>> install src_file=${src_file}"

	if [[ ${src_file_name} =~ ^.*\.tbz2$ ]]; then
		local extract_dir=$(tar -tf ${src_file} | head -1)
		local dst_extract_dir=${dst_dir}/${extract_dir}
		echo -e "[INFO] compress file, dst_extract_dir=${dst_extract_dir}"

		# compress file must include a sub dir
		if [[ -n ${extract_dir} ]] && [[ ${extract_dir} != "." ]]; then
			if [[ -d ${dst_extract_dir} ]]; then
				sudo_cmd_check_w $(dirname ${dst_extract_dir}) \
					"mv ${dst_extract_dir} ${dst_extract_dir}.bak"
			fi

			sudo_cmd "tar -jxf ${src_file} -C ${dst_dir}"
		fi
	else
		local cut_dir_num=$((${strip_dir_num}+1))
		local dst_file=${dst_dir}/$(echo ${src_file} | cut -d/ -f${cut_dir_num}-)

		if [[ -f ${dst_file} ]]; then
			sudo_cmd_check_w ${dst_dir} "mv ${dst_file} ${dst_file}.bak"
		fi
		if [[ ! -d $(dirname ${dst_file}) ]]; then
			sudo_cmd_check_w ${dst_dir} "mkdir -p $(dirname ${dst_file})"
		fi

		# Hard Link File
		sudo_cmd_check_w $(dirname ${dst_file}) "ln ${src_file} ${dst_file}"

		local filetype=$(file -b ${dst_file} | awk -F ',' '{print $1}')
		if [[ ${filetype} =~ Bourne-Again ]] \
			|| [[ ${filetype} =~ ELF ]]; then
			sudo_cmd_check_w ${dst_file} "chmod a+x ${dst_file}"
			echo -e "[INFO] regular exec file, copy to: ${dst_file} (+x)"
		else
			echo -e "[INFO] regular file, copy to: ${dst_file}"
		fi
	fi
}

function traverse_exec_uninstall()
{
#	echo -e "[DEBUG] ${FUNCNAME} $@"

	local src_file=$1
	local strip_dir_num=$2
	local dst_dir=$3

	echo -e "[INFO] >>> uninstall src_file=${src_file}"

	if [[ ${src_file_name} =~ ^.*\.tbz2$ ]]; then
		local extract_dir=$(tar -tf ${src_file} | head -1)
		local dst_extract_dir=${dst_dir}/${extract_dir}
		echo -e "[INFO] compress file, dst_extract_dir=${dst_extract_dir}"

		if [[ -n ${extract_dir} ]] && [[ ${extract_dir} != "." ]]; then
			if [[ -d ${dst_dir}/${dst_extract_dir} ]]; then
				#sudo_cmd "rm -rf ${dst_dir}/${dst_extract_dir}"
				echo -e "rm -rf ${dst_dir}/${dst_extract_dir}"
			fi

			if [[ -d ${dst_extract_dir}.bak ]]; then
				sudo_cmd_check_w $(dirname ${dst_extract_dir}) \
					"mv ${dst_extract_dir}.bak ${dst_extract_dir}"
			fi
		fi
	else
		local cut_dir_num=$((${strip_dir_num}+1))
		local dst_file=${dst_dir}/$(echo ${src_file} | cut -d/ -f${cut_dir_num}-)
		echo -e "[INFO] regular file, dst_file=${dst_file}"

		sudo_cmd "rm -rf ${bsp_file}"
		if [[ -f ${dst_file}.bak ]]; then
			sudo_cmd_check_w $(dirname ${dst_file}) "mv ${dst_file}.bak ${dst_file}"
		fi
	fi
}
